package org.jeecg.modules.demo.cashier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.api.UpmsHelloApi;
import org.jeecg.modules.demo.mall.entity.YxLevelProductDiscount;
import org.jeecg.modules.demo.mall.entity.YxMerchantStore;
import org.jeecg.modules.demo.mall.entity.YxStoreCategory;
import org.jeecg.modules.demo.mall.entity.YxStoreProduct;
import org.jeecg.modules.demo.mall.yxlevelproductdiscount.service.IYxLevelProductDiscountService;
import org.jeecg.modules.demo.mall.yxmerchantstore.service.IYxMerchantStoreService;
import org.jeecg.modules.demo.mall.yxstorecategory.service.IYxStoreCategoryService;
import org.jeecg.modules.demo.mall.yxstoreproduct.service.IYxStoreProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 收银端操作商品信息接口
 *
 * @Author: jeecg-boot
 * @Date: 2023-10-19
 * @Version: V1.0
 */

@RestController
@RequestMapping("/mall/cashierstoreproduct/cashierStoreProduct")
@Slf4j
public class CashierStoreProductController extends JeecgController<YxStoreProduct, IYxStoreProductService> {
    @Autowired
    private IYxStoreProductService yxStoreProductService;
    @Autowired
    private IYxStoreCategoryService yxStoreCategoryService;
    @Autowired
    private IYxMerchantStoreService yxMerchantStoreService;
    @Autowired
    private IYxLevelProductDiscountService yxLevelProductDiscountService;


    private final UpmsHelloApi upmsHelloApi;

    public CashierStoreProductController(UpmsHelloApi upmsHelloApi) {
        this.upmsHelloApi = upmsHelloApi;
    }

    /**
     * 商品 分页列表查询
     *
     * @param yxStoreProduct
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "商品分页列表查询", notes = "商品分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<YxStoreProduct>> queryPageList(YxStoreProduct yxStoreProduct,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       String merId) {
        if (StringUtils.isBlank(merId)) {
            return Result.error("商家id为空");
        }

        QueryWrapper<YxStoreProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", 0);
        queryWrapper.like("dept_ids", merId);
        if (StringUtils.isNotBlank(yxStoreProduct.getStoreName())) {
            queryWrapper.like("store_name", yxStoreProduct.getStoreName());
        }
        Page<YxStoreProduct> page = new Page<YxStoreProduct>(pageNo, pageSize);
        IPage<YxStoreProduct> pageList = yxStoreProductService.page(page, queryWrapper);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return Result.ok(pageList);
        }
        pageList.getRecords().forEach(product -> {
            YxStoreCategory byId = yxStoreCategoryService.getById(Integer.valueOf(product.getCateId()));
            if (ObjectUtils.isNotEmpty(byId)) {
                product.setCateName(byId.getCateName());
            }

            //查询商户商品的价格库存
            QueryWrapper<YxMerchantStore> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(merId)) {
                wrapper.eq("store_id", product.getId());
                wrapper.eq("merchant_id", merId);
                YxMerchantStore yxMerchantStore = yxMerchantStoreService.getOne(wrapper);
                //判断当前商品是否分配给当前商家
                if (ObjectUtils.isNotEmpty(yxMerchantStore)) {
                    //赋值库存价格
                    product.setPrice(yxMerchantStore.getPrice());
                    product.setStocks(yxMerchantStore.getStock());
                    product.setStatus(yxMerchantStore.getStatus());
                    product.setStoreId(yxMerchantStore.getId());
                }
            }
        });
        return Result.OK(pageList);
    }


    /**
     * 分类查询
     *
     * @param yxStoreCategory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/queryCategoryPage")
    public Result<IPage<YxStoreCategory>> queryPageList(YxStoreCategory yxStoreCategory,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        if (StringUtils.isBlank(yxStoreCategory.getMerId())) {
            return Result.error("商家id为空");
        }
        QueryWrapper<YxStoreCategory> queryWrapper = QueryGenerator.initQueryWrapper(yxStoreCategory, req.getParameterMap());
        queryWrapper.and(queryWrapper1 -> queryWrapper1.eq("mer_id", yxStoreCategory.getMerId()).or().apply("mer_id IS NULL"));
        queryWrapper.eq("is_del", 0);
        queryWrapper.orderByDesc("sort");
        Page<YxStoreCategory> page = new Page<YxStoreCategory>(pageNo, pageSize);
        IPage<YxStoreCategory> pageList = yxStoreCategoryService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @ApiOperation(value = "商家商品信息表-分页列表查询", notes = "商家商品信息表-分页列表查询")
    @GetMapping(value = "/listMer")
    public Result<IPage<YxMerchantStore>> queryPageList(YxMerchantStore yxMerchantStore,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "100") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<YxMerchantStore> queryWrapper = QueryGenerator.initQueryWrapper(yxMerchantStore, req.getParameterMap());
        queryWrapper.eq("merchant_id", yxMerchantStore.getMerId());
        //状态是上架的
//        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("create_time");
        Page<YxMerchantStore> page = new Page<YxMerchantStore>(pageNo, pageSize);
        IPage<YxMerchantStore> pageList = yxMerchantStoreService.page(page, queryWrapper);
        if (CollectionUtils.isEmpty(pageList.getRecords())) {
            return Result.ok(pageList);
        }
        pageList.getRecords().forEach(product -> {
            YxStoreProduct byId = yxStoreProductService.getById(product.getStoreId());
            if (byId != null) {
                product.setStoreName(byId.getStoreName());
            }
            //赋值库存价格
            YxLevelProductDiscount yxLevelProductDiscount = yxLevelProductDiscountService.queryLevelPrice(0, yxMerchantStore.getLevelId(), Integer.valueOf(yxMerchantStore.getMerId()), Long.valueOf(product.getStoreId()));
            if (yxLevelProductDiscount != null) {
                product.setLevelPrice(product.getPrice().multiply(yxLevelProductDiscount.getDiscount().divide(BigDecimal.valueOf(10), BigDecimal.ROUND_CEILING)).setScale(2, 4));
            }

        });
        return Result.OK(pageList);
    }


    @AutoLog(value = "yx_store_product-添加")
    @ApiOperation(value = "yx_store_product-添加", notes = "yx_store_product-添加")
    @PostMapping(value = "/addStoreProduct")
    public Result<String> addStoreProduct(@RequestBody YxStoreProduct yxStoreProduct) {
        yxStoreProduct.setDeptIds(String.valueOf(yxStoreProduct.getMerId()));
        yxStoreProductService.save(yxStoreProduct);
        YxMerchantStore merchantStore = new YxMerchantStore();
        merchantStore.setMerchantId(String.valueOf(yxStoreProduct.getMerId()));
        merchantStore.setStoreId(yxStoreProduct.getId());
        merchantStore.setStoreName(yxStoreProduct.getMerName());
        merchantStore.setStock(yxStoreProduct.getStock());
        merchantStore.setPrice(yxStoreProduct.getPrice());
        merchantStore.setName(yxStoreProduct.getMerName());
        merchantStore.setPersonalNum(yxStoreProduct.getPersonalNum());
        merchantStore.setStatus(yxStoreProduct.getStatus());
        yxMerchantStoreService.save(merchantStore);
        return Result.ok("添加成功");
    }

}
